package com.example.Project.user;

import com.example.Project.Class.MassOrganizations;
import com.example.Project.Class.queryMass;
import com.example.Project.user.Methods.ToExcel;
import com.example.Project.user.userHttpMethods.userMethods;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class QueryController {
    com.example.Project.user.userHttpMethods.userMethods userMethods = new userMethods();

    @FXML
    private AnchorPane Pane;

    @FXML
    private DatePicker birthdayEnd;

    @FXML
    private Label birthdayLabel;

    @FXML
    private DatePicker birthdayStart;

    @FXML
    private TextField id;

    @FXML
    private ContextMenu menu;

    @FXML
    private TextField name;

    @FXML
    private TextField phone;

    @FXML
    private Button queryButton;

    @FXML
    private TableView<MassOrganizations> table;
    @FXML
    private TableColumn<MassOrganizations, Integer> tableId;
    @FXML
    private TableColumn<MassOrganizations, String> tableName;
    @FXML
    private TableColumn<MassOrganizations, Date> tableBirthday;
    @FXML
    private TableColumn<MassOrganizations, String> tableType;
    @FXML
    private TableColumn<MassOrganizations, String> tablePhone;

    @FXML
    private ComboBox<String> type;

    @FXML
    public void initialize() {
        // 添加选项
        type.getItems().addAll("功能型", "爱好型", "艺术型");
    }

    java.time.LocalDate birStart;
    java.time.LocalDate birEnd;
    int Id;
    String nam;
    String pho;
    String typ;

    private void updateTableView(List<MassOrganizations> MassOrganizations) {
        this.table.getItems().addAll(MassOrganizations);
        this.tableId.setCellValueFactory(new PropertyValueFactory<>("Id"));
        this.tableName.setCellValueFactory(new PropertyValueFactory<>("Name"));
        this.tableBirthday.setCellValueFactory(new PropertyValueFactory<>("Birthday"));
        this.tableType.setCellValueFactory(new PropertyValueFactory<>("Type"));
        this.tablePhone.setCellValueFactory(new PropertyValueFactory<>("Phone"));
    }

    @FXML
    void query(MouseEvent event) throws IOException {
        if (!id.getText().isEmpty()) {
            Id = Integer.parseInt(id.getText());
        } else {
            Id = 0;
        }
        if (birthdayStart.getValue() != null) {
            birStart = birthdayStart.getValue();
        } else {
            birStart = LocalDate.of(1949, 10, 1);
        }
        if (birthdayEnd.getValue() != null) {
            birEnd = birthdayEnd.getValue();
        } else {
            birEnd = LocalDate.now();
        }
        if (!name.getText().isEmpty()) {
            nam = name.getText();
        } else {
            nam = null;
        }
        if (!phone.getText().isEmpty()) {
            pho = phone.getText();
        } else {
            pho = null;
        }
        if (type.getSelectionModel().getSelectedItem() != null) {
            typ = type.getSelectionModel().getSelectedItem();
            System.out.println(typ);
        }
        queryMass query = new queryMass(Id, nam, birStart, birEnd, typ, pho);
        table.getItems().clear();
        ObservableList<MassOrganizations> massOrganizationsList = userMethods.queryMassOrganization(query);
        updateTable(massOrganizationsList);
    }

    @FXML
    private Button toExcelButton;

    @FXML
    void toExcel(MouseEvent event) {
        ObservableList<MassOrganizations> tableData = table.getItems();
        List<MassOrganizations> massToExcel = new ArrayList<>(tableData);
        ToExcel.setInfo(massToExcel);
    }
    private void updateTable(List<MassOrganizations> MassOrganizations) {
        this.table.getItems().addAll(MassOrganizations);
        this.tableId.setCellValueFactory(new PropertyValueFactory<>("Id"));
        this.tableName.setCellValueFactory(new PropertyValueFactory<>("Name"));
        this.tableBirthday.setCellValueFactory(new PropertyValueFactory<>("Birthday"));
        this.tableType.setCellValueFactory(new PropertyValueFactory<>("Type"));
        this.tablePhone.setCellValueFactory(new PropertyValueFactory<>("Phone"));
    }
}
